package iscript.uiFramework.util
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	public class ElementUtil
	{
		
		public function ElementUtil()
		{
		
		}
		
		//{#region  ================清除显示对象===================
		/**
		 * 删除全部 Object中的子对象
		 */
		static public function removeAllItem(container:DisplayObjectContainer):void
		{
			if (container == null)
			{
				return;
			}
			for (var i:int = container.numChildren - 1; i >= 0; i--)
			{
				container.removeChildAt(0);
			}
		}
		
		static public function remove(container:DisplayObject):void
		{
			if (container.parent == null)
				return;
			container.parent.removeChild(container);
		}
		
		/**
		 * 删除一个显示对象
		 * @param	ContainerObj
		 */
		static public function clear(target1:DisplayObjectContainer, target2:DisplayObject):void
		{
			//try{ContainerObj.parent.removeChild(ContainerObj);}catch(e:*){}	
			if (target1 == null || target2 == null)
			{
				return;
			}
			try
			{
				target1.removeChild(target2);
			}
			catch (e:*)
			{
			}
		}
		
		/**
		 * 删除Zindex位置的显示对象  或者 Zindex  EndZindex 范围的 显示对象
		 * @param	ContainerObj
		 * @param	Zindex
		 * @param	EndZindex
		 */
		static public function removeByZindex(ContainerObj:*, StartZindex:int, EndZindex:* = null):void
		{
			if (ContainerObj == null)
			{
				return;
			}
			var num:int = StartZindex;
			if (EndZindex != null)
			{
				var _temp_:Number;
				EndZindex > ContainerObj.numChildren - 1 ? _temp_ = ContainerObj.numChildren - 1 : _temp_ = EndZindex;
				
				for (var i:int = _temp_; i >= num; i--)
				{
					try
					{
						ContainerObj.removeChild(ContainerObj.getChildAt(i))
					}
					catch (e:*)
					{
					}
				}
			}
			else
			{
				if (ContainerObj.numChildren - 1 >= num)
				{
					try
					{
						ContainerObj.removeChild(ContainerObj.getChildAt(num))
					}
					catch (e:*)
					{
					}
				}
			}
		}
		
		/**
		 * 删除类型相符的子对象
		 * type String  "[object Shape]"
		 */
		static public function removeByType(ContainerObj:*, type:String):void
		{
			if (ContainerObj == null)
			{
				return;
			}
			var objArr:Array = new Array();
			for (var i:int = 0; i < ContainerObj.numChildren; i++)
			{
				if (type != null)
				{
					if (ContainerObj.getChildAt(i).toString().toUpperCase() == type.toUpperCase())
					{
						objArr.push(ContainerObj.getChildAt(i).name);
					}
				}
			}
			for (var d:int = 0; d < objArr.length; d++)
			{
				try
				{
					ContainerObj.removeChild(ContainerObj.getChildByName(objArr[d]));
				}
				catch (e:*)
				{
				}
			}
			objArr = null;
		}
		
		/**
		 * 删除名字相符的子对象 Array多个子对象  包含对^ 名字的过滤使用
		 * @param	ContainerObj
		 * @param	...arg
		 */
		static public function removeByName(ContainerObj:*, ... arg):void
		{
			if (arg.length == 1 && arg[0].substring(0, 1) == "^")
			{
				
				var objArr:Array = new Array();
				var names:String = arg[0].substring(1);
				for (var j:int = 0; j < ContainerObj.numChildren; j++)
				{
					if (ContainerObj.getChildAt(j).name.substring(0, names.length) == arg[0].substring(1))
					{
						objArr.push(ContainerObj.getChildAt(j).name);
					}
				}
				for (var d:int = 0; d < objArr.length; d++)
				{
					try
					{
						ContainerObj.removeChild(ContainerObj.getChildByName(objArr[d]));
					}
					catch (e:*)
					{
					}
				}
				objArr = null;
				return;
			}
			for (var i:int = 0; i < arg.length; i++)
			{
				
				try
				{
					
					ContainerObj.removeChild(ContainerObj.getChildByName(arg[i]))
					
				}
				catch (e:*)
				{
				}
			}
		}
		
		//}#endregion
		
		/**
		 * 根据name获得 显示对象列表  包含对^ 的过滤
		 * @param	ContainerObj
		 * @param	...arg
		 * @return
		 */
		static public function getDomByName(ContainerObj:*, ... arg):Array
		{
			var objArr:Array = new Array();
			if (arg.length == 1 && arg[0].substring(0, 1) == "^")
			{
				var names:String = arg[0].substring(1);
				for (var j:int = 0; j < ContainerObj.numChildren; j++)
				{
					if (ContainerObj.getChildAt(j).name.substring(0, names.length) == arg[0].substring(1))
					{
						objArr.push(ContainerObj.getChildAt(j));
					}
				}
			}
			else
			{
				for (j = 0; j < ContainerObj.numChildren; j++)
				{
					if (ContainerObj.getChildAt(j).name == arg[0])
					{
						objArr.push(ContainerObj.getChildAt(j));
					}
				}
			}
			return objArr
		}
		
		/**
		 * 根据"[object Shape]"获得 显示对象列表  包含对^ 的过滤
		 * @param	ContainerObj
		 * @param	type
		 * @return
		 */
		static public function getDomByType(ContainerObj:*, type:String):Array
		{
			var objArr:Array = new Array();
			for (var i:int = 0; i < ContainerObj.numChildren; i++)
			{
				if (type != null)
				{
					if (ContainerObj.getChildAt(i).toString().toUpperCase() == type.toUpperCase())
					{
						objArr.push(ContainerObj.getChildAt(i));
					}
				}
			}
			return objArr;
		}
	}

}